9 namespace Core.TypeCast
12 using System.Reflection;
14 using System.Runtime.CompilerServices;
20 public static partial class ObjectExtension
33 [MethodImpl(MethodImplOptions.AggressiveInlining)]
34 public static TOut Transform<TOut>(
this TOut
self, Enum functionAlias)
36 return Transform<TOut>(
self:
self, model: null, functionAlias: functionAlias.ToString());
53 [MethodImpl(MethodImplOptions.AggressiveInlining)]
54 public static TOut Transform<TOut>(
this TOut
self,
object model, Enum functionAlias,
bool strictTypeCheck =
true,
bool withContext =
false)
56 return Transform<TOut>(
self:
self, model: model, functionAlias: functionAlias.ToString(), withContext: withContext);
69 [MethodImpl(MethodImplOptions.AggressiveInlining)]
70 public static TOut Transform<TOut>(
this object self, Enum functionAlias)
72 return (TOut)Transform(
self:
self, model: null, typeBase: null, typeTo: typeof(TOut), functionAlias: functionAlias.ToString(), throwException:
false);
88 [MethodImpl(MethodImplOptions.AggressiveInlining)]
89 public static TOut Transform<TOut>(
this object self,
object model, Enum functionAlias,
bool withContext =
false)
91 return (TOut)Transform(
self:
self, model: null, typeBase: null, typeTo: typeof(TOut), functionAlias: functionAlias.ToString(), throwException:
false, withContext: withContext);
107 [MethodImpl(MethodImplOptions.AggressiveInlining)]
108 public static TOut Transform<TOut>(
this TOut
self,
object model,
string functionAlias = null,
bool withContext =
false)
110 return (TOut)Transform(
self:
self, model: model, typeBase: null, typeTo: typeof(TOut), functionAlias: functionAlias, throwException:
false, withContext: withContext);
127 [MethodImpl(MethodImplOptions.AggressiveInlining)]
128 public static TOut Transform<TBase, TOut>(
this object self,
object model = null,
string functionAlias = null,
bool strictTypeCheck =
false,
bool withContext =
false)
130 CheckTransformTypes<TOut>(
self, strictTypeCheck);
131 return (TOut)Transform<TBase, object, TOut>(
self:
self, model: model, functionAlias: functionAlias, strictTypeCheck: strictTypeCheck, withContext: withContext);
134 [MethodImpl(MethodImplOptions.AggressiveInlining)]
135 public static TOut Transform<TBase, TIn, TOut>(
this object self,
object model = null,
string functionAlias = null,
bool strictTypeCheck =
false,
bool withContext =
false)
137 CheckTransformTypes<TOut>(
self, strictTypeCheck);
138 Converter converter = null;
139 return (TOut)Transform<TIn>(
self: (TIn)
self, converter: out converter, model: model, typeBase: typeof(TBase), typeTo: typeof(TOut), functionAlias: functionAlias, throwException:
false, withContext: withContext);
156 [MethodImpl(MethodImplOptions.AggressiveInlining)]
157 public static TOut Transform<TOut>(
this TOut
self, Type typeBase,
object model = null,
string functionAlias = null,
bool withContext =
false)
159 return (TOut)Transform(
self:
self, model: model, typeBase: null, typeTo: typeof(TOut), functionAlias: functionAlias, throwException:
false, withContext: withContext);
178 [MethodImpl(MethodImplOptions.AggressiveInlining)]
179 public static object Transform<TBase>(
this object self,
object model = null, Type typeBase = null, Type typeTo = null,
string functionAlias = null,
bool throwException =
true,
bool withContext =
false)
181 return Transform(
self:
self, model: model, typeBase: typeof(TBase), typeTo: typeTo, functionAlias: functionAlias, throwException: throwException, withContext: withContext);
199 [MethodImpl(MethodImplOptions.AggressiveInlining)]
200 public static object Transform(
this object self,
object model = null, Type typeBase = null, Type typeTo = null,
string functionAlias = null,
bool throwException =
true,
bool withContext =
false)
202 Converter converter = null;
203 return Transform(
self:
self, converter: out converter, model: model, typeBase: typeBase, typeTo: typeTo, functionAlias: functionAlias, throwException: throwException, withContext: withContext);
222 [MethodImpl(MethodImplOptions.AggressiveInlining)]
223 public static object Transform<TIn>(
this TIn
self, out Converter converter,
object model = null, Type typeBase = null, Type typeTo = null,
string functionAlias = null,
bool throwException =
true,
bool withContext =
false)
225 object result = null;
227 typeTo = typeTo ?? typeBase?.GetTypeInfo().GetReturnParameterType();
228 if(typeof(TIn) != objectType && typeBase?.GetTypeInfo().IsInvokableWithParameters(typeTo,
self.GetType(), model?.GetType()) ==
false)
230 throw new ConverterException(
ConverterCause.DelegateArgumentWrongType);
232 GetConverterOrDefault<TIn, object>(
self, out converter, out result, typeArgument: model?.GetType(), typeTo: functionAlias != null ? null : typeTo, typeBase: typeBase,
233 throwException: throwException, unboxObjectType: typeof(TIn) != objectType, attributeName: functionAlias);
235 InvokeConvert(
self, out result, model, throwException: throwException, converter: converter, contextInstance: (withContext ?
new ConvertContext(model) : null));
251 [MethodImpl(MethodImplOptions.AggressiveInlining)]
252 public static TOut Transform<TIn, TOut>(
this TIn
self, Func<TIn, TOut> converterAction, Enum addWithAttributeName,
bool throwException =
true)
254 return Transform<TIn, TOut>(
self:
self, converterAction: converterAction, addWithAttributeName: addWithAttributeName.ToString(), throwException: throwException);
268 public static TOut Transform<TIn, TOut>(
this TIn
self, Func<TIn, TOut> converterAction,
string addWithAttributeName = null,
bool throwException =
true)
270 TOut result =
default(TOut);
271 Converter converter = null;
272 converter = ConverterCollection.CurrentInstance.Factory.Create<TIn, TOut>(converterAction);
273 var baseType = converterAction?.Target?.GetType().DeclaringType;
275 if(String.IsNullOrWhiteSpace(addWithAttributeName) ==
false)
278 converter.Attribute =
new ConverterAttribute(loadOnDemand:
false, name: addWithAttributeName, nameSpace: baseType.Namespace);
279 ConverterCollection.CurrentInstance.Add(converter, allowDisambiguates:
true);
282 InvokeConvert(
self:
self, result: out result, defaultValue:
default(TOut), throwException: throwException, converter: converter);
303 [MethodImpl(MethodImplOptions.AggressiveInlining)]
304 public static bool TryTransform(
this object self, out
object result,
object model = null, Type typeBase = null, Type typeTo = null,
string functionAlias = null,
bool throwException =
false,
bool withContext =
false)
306 Converter converter = null;
307 result = Transform(
self:
self, converter: out converter, model: model, typeBase: typeBase, typeTo: typeTo, functionAlias: functionAlias, throwException: throwException, withContext: withContext);
308 return converter != null;
329 [MethodImpl(MethodImplOptions.AggressiveInlining)]
330 public static bool TryTransform<TIn, TOut>(
this TIn
self, out TOut result,
object model = null, Type typeBase = null,
string functionAlias = null,
bool throwException =
false,
bool withContext =
false)
332 Converter converter = null;
333 result = (TOut)Transform(
self:
self, converter: out converter, model: model, typeBase: typeBase, typeTo: typeof(TOut), functionAlias: functionAlias, throwException: throwException, withContext: withContext);
334 return converter != null;
353 [MethodImpl(MethodImplOptions.AggressiveInlining)]
354 public static bool TryTransform<TBase, TOut>(
this object self, out TOut result,
object model = null,
string functionAlias = null,
bool throwException =
false,
bool withContext =
false)
356 Converter converter = null;
357 result = (TOut)Transform(
self:
self, converter: out converter, model: model, typeBase: typeof(TBase), typeTo: typeof(TOut), functionAlias: functionAlias, throwException: throwException, withContext: withContext);
358 return converter != null;
378 [MethodImpl(MethodImplOptions.AggressiveInlining)]
379 public static bool TryTransform<TBase, TIn, TOut>(
this TIn
self, out TOut result,
object model = null,
string functionAlias = null,
bool throwException =
false,
bool withContext =
false)
381 Converter converter = null;
382 result = (TOut)Transform<TIn>(
self:
self, converter: out converter, model: model, typeBase: typeof(TBase), typeTo: typeof(TOut), functionAlias: functionAlias, throwException: throwException, withContext: withContext);
383 return converter != null;
394 [MethodImpl(MethodImplOptions.AggressiveInlining)]
395 private static void CheckTransformTypes<TOut>(
object self,
bool strictTypeCheck)
397 var sameOrSimilar =
self.GetType().GetTypeInfo().IsSameOrSimilar<TOut>();
398 if((strictTypeCheck ==
true && sameOrSimilar ==
TypeMatch.None))
400 throw new ConverterException(
ConverterCause.TransformRequiresEqualInOutTypes);
TypeMatch
An enumerating of values containing a rough 2bit classification of relationship between two types ...
ConverterCause
Contains the reasons for a ConverterException to be raised.